package com.tzbank.poc.controller;


import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.Map;

/**
 * @Description: 面向切面日志功能
 * Create by li_zheng2 on 2019/1/22.
 */
@Component // 将对象交由spring进行管理
@Aspect //代表此类为一个切面类
public class LogAspectAop {

    private static Logger log = LoggerFactory.getLogger(LogAspectAop.class);

    @Before("within(com.tzbank.poc.controller.*)")
    public void before(JoinPoint joinPoint){
        Object args[] = joinPoint.getArgs();
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        log.info("{}.{} :请求参数:{}",method.getDeclaringClass().getName(),method.getName(),StringUtils.join(args,";"));
    }

    @AfterReturning(value ="within(com.tzbank.poc.controller.*)", returning = "rvt")
    public void after(JoinPoint joinPoint,Object rvt){
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        log.info("{}.{} :返回数据:{}",method.getDeclaringClass().getName(),method.getName(),new Gson().toJson(rvt));
    }
}
